package com.da.auth.controller;

import com.da.auth.entity.vo.UserDetailsVO;
import com.da.auth.redis.RedisCache;
import com.da.common.entity.PageResult;
import com.da.common.enums.ResultEnum;
import com.da.common.exception.ParameterException;
import com.da.common.utils.DailyAimResult;
import com.da.auth.convert.DailyRoleConverter;
import com.da.auth.entity.DailyRole;
import com.da.auth.entity.dto.DailyRoleDTO;
import com.da.auth.service.DailyRoleService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @Author: hyacinth
 * @CreateTime: 2025-01-20
 * @Version: 1.0
 */
@RestController
@RequestMapping("/role")
@Slf4j
public class DailyRoleController {

    private final DailyRoleService dailyRoleService;
    private final RedisCache redisCache;

    public DailyRoleController(DailyRoleService dailyRoleService, RedisCache redisCache) {
        this.dailyRoleService = dailyRoleService;
        this.redisCache = redisCache;
    }

    @PostMapping("/createRole")
    @Operation(summary = "新增角色", description = "新增角色")
    public DailyAimResult<?> createRole(@RequestBody DailyRoleDTO dailyRoleDTO) {
        Optional.ofNullable(dailyRoleDTO).orElseThrow(() -> new ParameterException(ResultEnum.FAIL.getCode(), "新增角色信息不能为空！"));
        Boolean isCreateRole = dailyRoleService.createRole(dailyRoleDTO);
        if (isCreateRole) {
            return DailyAimResult.success(ResultEnum.SUCCESS.getCode(), "新增角色成功", true);
        } else {
            return DailyAimResult.error(ResultEnum.FAIL.getCode(), "新增角色失败");
        }
    }

    @GetMapping("/queryPageAllRole")
    @Operation(summary = "分页查询查询所有角色", description = "分页查询查询所有角色")
    public DailyAimResult<?> queryPageAllRole(@RequestParam(defaultValue = "1") int pageNum,
                                              @RequestParam(defaultValue = "10") int pageSize) {
        PageResult<DailyRole> dailyRolePageResult = dailyRoleService.queryPageAllRole(pageNum, pageSize);
        return DailyAimResult.success(ResultEnum.SUCCESS.getCode(), "查询成功", dailyRolePageResult);
    }

    @GetMapping("/queryRoleInfoById/{id}")
    @Operation(summary = "根据id查询角色", description = "根据id查询角色")
    public DailyAimResult<?> queryRoleInfoById(@PathVariable String id) {
        Optional.ofNullable(id).orElseThrow(() -> new ParameterException(ResultEnum.FAIL.getCode(), "id不能为空！"));
        DailyRole dailyRole = dailyRoleService.queryRoleInfoById(id);
        return !ObjectUtils.isEmpty(dailyRole) ? DailyAimResult.success("查询用户成功", dailyRole) : DailyAimResult.error(ResultEnum.SUCCESS.getCode(), "");
    }

    @PutMapping("/updateRole")
    @Operation(summary = "修改角色", description = "修改角色")
    public DailyAimResult<?> updateRole(@RequestBody DailyRoleDTO dailyRoleDTO) {
        Optional.ofNullable(dailyRoleDTO).orElseThrow(() -> new ParameterException(ResultEnum.FAIL.getCode(), "修改角色信息不能为空！"));
        DailyRole dailyRole = DailyRoleConverter.INSTANCE.convertToEntity(dailyRoleDTO);
        Boolean isCreateRole = dailyRoleService.updateRole(dailyRole);
        if (isCreateRole) {
            return DailyAimResult.success(ResultEnum.SUCCESS.getCode(), "修改角色成功", true);
        } else {
            return DailyAimResult.error(ResultEnum.FAIL.getCode(), "修改角色失败");
        }
    }

    @DeleteMapping("/deletedRole/{id}")
    @Operation(summary = "删除角色", description = "删除角色")
    public DailyAimResult<?> deletedRole(@PathVariable String id) {
        Optional.ofNullable(id).orElseThrow(() -> new ParameterException(ResultEnum.FAIL.getCode(), "删除角色信息不能为空！"));
        Boolean isCreateRole = dailyRoleService.deletedRole(id);
        if (isCreateRole) {
            return DailyAimResult.success(ResultEnum.SUCCESS.getCode(), "删除角色成功", true);
        } else {
            return DailyAimResult.error(ResultEnum.FAIL.getCode(), "删除角色失败");
        }
    }

    @DeleteMapping("/batchDeletedRole")
    @Operation(summary = "批量删除角色", description = "批量删除角色")
    public DailyAimResult<?> batchDeletedRole(@RequestBody List<String> roleIdList) {
        Optional.ofNullable(roleIdList).orElseThrow(() -> new ParameterException(ResultEnum.FAIL.getCode(), "roleIdList不能为空！"));
        Boolean isCreateRole = dailyRoleService.batchDeletedRole(roleIdList);
        if (isCreateRole) {
            return DailyAimResult.success(ResultEnum.SUCCESS.getCode(), "删除角色成功", true);
        } else {
            return DailyAimResult.error(ResultEnum.FAIL.getCode(), "删除角色失败");
        }
    }

    @PostMapping("/getRedisData")
    @Operation(summary = "getRedisData", description = "getRedisData")
    public DailyAimResult<?> getRedisData(String key) {
        Object cacheObject = redisCache.getCacheObject(key);
        if (cacheObject != null) {
            UserDetailsVO userInfo = null;
            if (cacheObject instanceof UserDetailsVO) {
                userInfo = (UserDetailsVO) cacheObject;
            }
            return DailyAimResult.success("查询成功", userInfo);
        } else {
            return DailyAimResult.error(ResultEnum.SUCCESS.getCode(), "");
        }
    }
}
